//
//  FQDLabFeedTitleView.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabFeedTitleView.h"
#import "FQDLabTitleImageView.h"
#import "FQDRestModel.h"

#import "Masonry.h"

@interface FQDLabFeedTitleView ()

// 内容图片
@property (nonatomic, strong) FQDLabTitleImageView *titleImageView;
// 内容标题
@property (nonatomic, strong) UILabel *titleLabel;
// 内容描述
@property (nonatomic, strong) UILabel *descLabel;

@end

@implementation FQDLabFeedTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _hideTitleImage = NO;
        [self prepareUI];
        [self prepareConstraints];
        [self updateTitleImage];
    }
    return self;
}

- (void)prepareUI
{
    
    _titleImageView = [[FQDLabTitleImageView alloc] init];
    [self addSubview:_titleImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(16.0f);
    _titleLabel.textColor = FQDFeedTitleTextColor;
    _titleLabel.numberOfLines = 2;
    _titleLabel.preferredMaxLayoutWidth = 300.0f;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.clipsToBounds = YES;
    [self addSubview:_titleLabel];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(14.0f);
    _descLabel.textColor = FQDFeedDetailTextColor;
    _descLabel.numberOfLines = 3;
    _descLabel.preferredMaxLayoutWidth = 300.0f;
    _descLabel.backgroundColor = [UIColor whiteColor];
    _descLabel.clipsToBounds = YES;
    [self addSubview:_descLabel];
    
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleImageView.mas_bottom).offset(10);
        make.left.equalTo(weakSelf).offset(10);
        make.width.equalTo(weakSelf).offset(-20);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(9);
        make.left.equalTo(weakSelf).offset(10);
        make.width.equalTo(weakSelf.titleLabel);
        make.bottom.equalTo(weakSelf);
    }];
}

- (void)updateTitleImage
{
    CGFloat imageHeight;
    if (self.hideTitleImage) {
        imageHeight = 0;
        self.titleImageView.hidden = YES;
    } else {
        imageHeight = 200.0f;
        self.titleImageView.hidden = NO;
    }
    
    WeakSelf;
    [self.titleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(weakSelf);
        make.height.equalTo(@(imageHeight));
    }];
}

#pragma mark - Getters and Setters
- (void)setPost:(FQDPost *)post
{
    _post = post;
    
    self.titleImageView.post = post;
    [self.titleLabel setText:self.post.title];
    [self.descLabel setText:self.post.desc];
}

- (void)setHideTitleImage:(BOOL)hideImage
{
    _hideTitleImage = hideImage;
    
    [self updateTitleImage];
}

@end
